Conditions | 1 |
Paths | 4 |
Total Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 20 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* |
||
70 | link: function (scope, element, attrs) { |
||
71 | var canvas = element.find('canvas')[0]; |
||
72 | var parent = canvas.parentElement; |
||
73 | var scale = 0; |
||
74 | var ctx = canvas.getContext('2d'); |
||
75 | |||
76 | var width = parseInt(scope.width, 10); |
||
77 | var height = parseInt(scope.height, 10); |
||
78 | |||
79 | canvas.width = width; |
||
80 | canvas.height = height; |
||
81 | |||
82 | scope.signaturePad = new SignaturePad(canvas); |
||
83 | |||
84 | scope.setDataUrl = function(dataUrl) { |
||
85 | var ratio = Math.max(window.devicePixelRatio || 1, 1); |
||
86 | |||
87 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
88 | ctx.scale(ratio, ratio); |
||
89 | |||
90 | scope.signaturePad.clear(); |
||
91 | scope.signaturePad.fromDataURL(dataUrl); |
||
92 | |||
93 | $timeout(function () { |
||
94 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
95 | ctx.scale(1 / scale, 1 / scale); |
||
96 | }); |
||
97 | }; |
||
98 | |||
99 | scope.$watch('disabled', function (val) { |
||
100 | val ? scope.signaturePad.off() : scope.signaturePad.on(); |
||
101 | }); |
||
102 | |||
103 | var calculateScale = function() { |
||
104 | var scaleWidth = Math.min(parent.clientWidth / width, 1); |
||
105 | var scaleHeight = Math.min(parent.clientHeight / height, 1); |
||
106 | |||
107 | var newScale = Math.min(scaleWidth, scaleHeight); |
||
108 | |||
109 | if (newScale === scale) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | var newWidth = width * newScale; |
||
114 | var newHeight = height * newScale; |
||
115 | canvas.style.height = Math.round(newHeight) + "px"; |
||
116 | canvas.style.width = Math.round(newWidth) + "px"; |
||
117 | |||
118 | scale = newScale; |
||
119 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
120 | ctx.scale(1 / scale, 1 / scale); |
||
121 | }; |
||
122 | |||
123 | var resizeIH = $interval(calculateScale, 200); |
||
124 | scope.$on('$destroy', function () { |
||
125 | $interval.cancel(resizeIH); |
||
126 | resizeIH = null; |
||
127 | }); |
||
128 | |||
129 | angular.element($window).bind('resize', calculateScale); |
||
130 | scope.$on('$destroy', function () { |
||
131 | angular.element($window).unbind('resize', calculateScale); |
||
132 | }); |
||
133 | |||
134 | calculateScale(); |
||
135 | |||
136 | element.on('touchstart', onTouchstart); |
||
137 | element.on('touchend', onTouchend); |
||
138 | |||
139 | function onTouchstart(event) { |
||
140 | scope.$apply(function () { |
||
141 | // notify that drawing has started |
||
142 | scope.notifyDrawing({ drawing: true }); |
||
143 | }); |
||
144 | event.preventDefault(); |
||
145 | } |
||
146 | |||
147 | function onTouchend(event) { |
||
148 | scope.$apply(function () { |
||
149 | // updateModel |
||
150 | scope.updateModel(); |
||
151 | |||
152 | // notify that drawing has ended |
||
153 | scope.notifyDrawing({ drawing: false }); |
||
154 | }); |
||
155 | event.preventDefault(); |
||
156 | } |
||
157 | } |
||
158 | }; |
||
164 |